package mario.game.graymachine.core;

import mario.game.graymachine.R;
import mario.game.graymachine.sprite.Fly;
import mario.game.graymachine.sprite.GLSprite;
import mario.game.graymachine.sprite.Player;
import mario.game.graymachine.sprite.Sprite1;
import mario.game.graymachine.sprite.Sprite3;

public class Scene {
	
	static final int MAX_SPRITS=48;
	static final int MAX_BULLETS=64;

	GLSprite[] bg1=new GLSprite[2];
	GLSprite bg1group=new GLSprite(this);
	GLSprite bg2;
	
	public GLSprite[] mSpritesRes=new GLSprite[5];
	GLSprite[] mBullets = new GLSprite[MAX_BULLETS];
	
	GLSprite[] mSprites = new GLSprite[MAX_SPRITS];
	public Player player;
	public int bg1CamaraX;
	int camara2;
	public static int bg1CamaraXIncrement=4;
	private static int index=0;

	public Scene(){
		bg1[0]=new GLSprite(this);
		bg1[0].setResourceId(R.drawable.background);
		bg1[0].width=512;
		bg1[0].height=512;
		bg1[1]=new GLSprite(this);
		bg1[1].setResourceId(R.drawable.background);
		bg1[1].width=512;
		bg1[1].height=512;
		mSpritesRes[0]=new Player(this);
		mSpritesRes[1]=new Sprite1(this);
		mSpritesRes[2]=new Fly(this);
		mSpritesRes[3]=new GLSprite(R.drawable.dead1);
		mSpritesRes[4]=new GLSprite(R.drawable.dead2);
		
		Player pla = new Player(this);
		pla.setTextureNames(mSpritesRes[0].getTextureNames());
		player=pla;
	}
	
	/**
	 * when time go on , events happen.
	 * @param time int 1 is a frame, unsually 60 frame is a secend.
	 */
	static int sceneTime=0;
	void update(){
		sceneTime++;
		if(sceneTime%20==1||sceneTime%20==10){
			double r = Math.random();
			if(r>.75f){
				Sprite1 spri= getSpri1();
				if(spri!=null){
					spri.setup();
					spri.x=480;
					spri.y=(float) ((r-.75f)*1230);
					spri.setTextureNames(mSpritesRes[1].getTextureNames());
				}
			}else if(r>.5f&&r<.75f){
				Sprite3 spri= getSpri3();
				if(spri!=null){
					spri.setup();
					spri.x=480;
					spri.y=(float) ((r-.5f)*1230);
					spri.setTextureNames(mSpritesRes[1].getTextureNames());
				}
			}else{
				Fly fly=getSpri2();
				if(fly!=null){
					fly.setup();
					fly.x=480;
					fly.y=(float)(r*630)-fly.height/2;
					fly.setTextureNames(mSpritesRes[2].getTextureNames());
				}
			}
		}
	}
	

	private Sprite1 getSpri1(){
		for(int i=0;i<MAX_SPRITS;i++){
			if(mSprites[i]!=null&&mSprites[i].isDead){
				if(mSprites[i] instanceof Sprite1){
					return (Sprite1) mSprites[i];
				}
			}
		}
		for(int i=0;i<MAX_SPRITS;i++){
			if(mSprites[i]==null){
				mSprites[i]=new Sprite1(this);
				return (Sprite1) mSprites[i];
			}
		}
		for(int i=0;i<MAX_SPRITS;i++){
			if(mSprites[i].isDead){
				mSprites[i]=new Sprite1(this);
				return (Sprite1) mSprites[i];
			}
		}
		return null;
	}
	private Fly getSpri2(){
		for(int i=0;i<MAX_SPRITS;i++){
			if(mSprites[i]!=null&&mSprites[i].isDead){
				if(mSprites[i] instanceof Fly){
					return (Fly) mSprites[i];
				}
			}
		}
		for(int i=0;i<MAX_SPRITS;i++){
			if(mSprites[i]==null){
				mSprites[i]=new Fly(this);
				return (Fly) mSprites[i];
			}
		}
		for(int i=0;i<MAX_SPRITS;i++){
			if(mSprites[i].isDead){
				mSprites[i]=new Fly(this);
				return (Fly) mSprites[i];
			}
		}
		return null;
	}
	
	private Sprite3 getSpri3(){
		for(int i=0;i<MAX_SPRITS;i++){
			if(mSprites[i]!=null&&mSprites[i].isDead){
				if(mSprites[i] instanceof Sprite3){
					return (Sprite3) mSprites[i];
				}
			}
		}
		for(int i=0;i<MAX_SPRITS;i++){
			if(mSprites[i]==null){
				mSprites[i]=new Sprite3(this);
				return (Sprite3) mSprites[i];
			}
		}
		for(int i=0;i<MAX_SPRITS;i++){
			if(mSprites[i].isDead){
				mSprites[i]=new Sprite3(this);
				return (Sprite3) mSprites[i];
			}
		}
		return null;
	}
	
	private GLSprite getSprite(GLSprite spri){
		index++;
		if(index==30) index=0;
		if(mSprites[index]==null){
			mSprites[index]=new Fly(this);
		}
		return mSprites[index];
	}
	
	public void addBullets(GLSprite res,int vx,int vy,float x,float y,int w,int h,int type){
		for(int i=0;i<Scene.MAX_BULLETS;i++){
			if(mBullets[i]==null){
				mBullets[i]=new GLSprite(res.getResourceId());
				mBullets[i].velocityX=vx;
				mBullets[i].velocityY=vy;
				mBullets[i].x=x;
				mBullets[i].y=y;
				mBullets[i].setTextureName(res.getTextureName());
				mBullets[i].height=h;
				mBullets[i].width=w;
				mBullets[i].type=type;
				return;
			}
			if(mBullets[i].isDead){
				mBullets[i].setup();
				mBullets[i].velocityX=vx;
				mBullets[i].velocityY=vy;
				mBullets[i].x=x;
				mBullets[i].y=y;
				mBullets[i].setTextureName(res.getTextureName());
				mBullets[i].height=h;
				mBullets[i].width=w;
				mBullets[i].type=type;
				return;
			}
		}
	}
	public void addBullets(GLSprite res,boolean charse){
		
	}

	public void setup() {
		// TODO Auto-generated method stub
		sceneTime=0;
		Player pla = new Player(this);
		pla.setTextureNames(mSpritesRes[0].getTextureNames());
		player=pla;
	}
}
